#include "RingQueue.hpp"
#include "Thread.hpp"
#include "Task.hpp"
#include <string>
#include <vector>
#include <unistd.h>
#include <ctime>
int a = 10;
using namespace ThreadModule;
using namespace std;
using ringqueue_t = RingQueueu<Task>;

void PrintHello()
{
    cout << "hello" << endl;
}
void Consumer(ringqueue_t &rq)
{
    while (true)
    {
        Task t;
        rq.Pop(&t);
        t();
    }
}
void Productor(ringqueue_t &rq)
{
    
    while (true)
    {
        sleep(1);
        rq.Enqueue(Download);
        
    }
}
void InitComm(std::vector<Thread<ringqueue_t>> *threads, int num, ringqueue_t &rq, func_t<ringqueue_t> func)
{
    for (int i = 0; i < num; i++)
    {
        std::string name = "thread-" + std::to_string(i + 1);
        threads->emplace_back(func, rq, name);
        //(*threads)[threads->size()-1].Start();
        // threads->back().Start();
    }
}
void InitConsumer(std::vector<Thread<ringqueue_t>> *threads, int num, ringqueue_t &rq)
{
    InitComm(threads, num, rq, Consumer);
}

void InitProductor(std::vector<Thread<ringqueue_t>> *threads, int num, ringqueue_t &rq)
{
    InitComm(threads, num, rq, Productor);
}
void WaitAllThread(std::vector<Thread<ringqueue_t>> threads)
{
    for(auto thread : threads)
    {
        thread.Join();
    }
}
void StartAll(vector<Thread<ringqueue_t>>& threads)
{
    for(auto& thread : threads)
    {
        thread.Start();
    }
}
int main()
{
    ringqueue_t *bq = new ringqueue_t(10);
    std::vector<Thread<ringqueue_t>> threads;
    InitConsumer(&threads, 1, *bq);
    InitProductor(&threads, 1, *bq);
    StartAll(threads);
    WaitAllThread(threads);
    return 0;
}